#include "onewindow.h"
#include "build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/ui_onewindow.h"
#include "ui_onewindow.h"


#include <iostream>
using namespace std;

//模运算
int mod(int a, int b, int n) {
    int temp=1;
    for (int i = 0; i < b; i++) {
        temp = ((a * temp) % n + n) % n;
    }
    return temp;
}

// 快速模幂算法
int fastModularExponentiation(int base, int exponent, int modulus) {
    int result = 1;
    base %= modulus;
    while (exponent > 0) {
        if (exponent % 2 == 1)
            result = (result * base) % modulus;
        exponent >>= 1;
        base = (base * base) % modulus;
    }
    return result;
}

// 扩展的欧几里得算法
int extendedEuclidean(int a, int b, int& x, int& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int gcd = extendedEuclidean(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return gcd;
}

// 求解模 n 意义下的逆元
int modInverse(int a, int n) {
    int x, y;
    int gcd = extendedEuclidean(a, n, x, y);
    if (gcd != 1) {
        cout << "逆元不存在" << endl;
        return -1;  // 逆元不存在
    }
    else {
        return (x % n + n) % n;  // 确保返回值在 [0, n) 范围内
    }
}

int RSA(int c) {
    int p = 101;
    int q = 113;
    int e = 3533;

    int m = (p - 1) * (q - 1);
    int n = p * q;
    int inverse = modInverse(e, m);
    int mess = mod(c, inverse, n);

    return mess;
}
